﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;
using System.Text;

namespace Web
{
    public partial class ReadLog : System.Web.UI.Page
    {
        class AuthedForm
        {
            public string Fromer;
            public DateTime AuthDate;
            public string Toer;
        }
        protected StringBuilder sb = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            string fileId = Request.QueryString["fileId"];

            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            System.Collections.Generic.IEnumerable<AuthedForm> list = ctx.ExecuteQuery<AuthedForm>(string.Format(@"SELECT b.Name as Fromer,a.AuthDate,c.Name as Toer
FROM FileAuth a,Employee b,Employee c
where a.FileId={0} and a.FromId=b.Id and a.ToId=c.Id
order by AuthDate;",fileId));
            AuthedForm authedForm =null;
            string td0 ="";
            string td1 = "";
            string td2 = "";
            int i = 0;
            foreach(AuthedForm af in list)
            {
                if(authedForm==null)
                {
                    authedForm =af;
                    td0 = af.Fromer;
                    td1 = af.AuthDate.ToString("yyyy-MM-dd HH:mm:ss");
                    td2 = af.Toer;
                }
                else if (authedForm.Fromer == af.Fromer && af.AuthDate.Subtract(authedForm.AuthDate).Minutes<120)
                {
                    if(i<100)
                    {
                        td2 += "," + af.Toer;
                        i++;
                    }
                    else if(i==100)
                    {
                        td2 += "……";
                        i++;
                    }
                }
                else
                {
                    sb.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", td0, td1, td2);
                    i = 0;
                    td0 = af.Fromer;
                    td1 = af.AuthDate.ToString("yyyy-MM-dd HH:mm:ss");
                    td2 = af.Toer;
                }
            }
            sb.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", td0, td1, td2);

            var v = new FileController();
            IQueryable<FileReadLogDTO> fileReadLogDtos = v.FileReadLog(int.Parse(fileId));
            rptList.DataSource = fileReadLogDtos;
            rptList.DataBind();
        }
    }
}